package com.CST.service.impl.cb;

import com.CST.entity.cb.JxSalaryProject;
import com.CST.mapper.cst.cb.JxCostMapper;
import com.CST.mapper.salary.JxSalaryProjectMapper;
import com.CST.service.cb.JxSalaryProjectService;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author nmx
 * @since 2023-06-30
 */
@Service
@Slf4j
public class JxSalaryProjectServiceImpl extends ServiceImpl<JxSalaryProjectMapper, JxSalaryProject> implements JxSalaryProjectService {

    @Autowired
    private JxSalaryProjectMapper jxSalaryProjectMapper;

    @Autowired
    private JxCostMapper jxCostMapper;

    @Override
    @Transactional
    public String getSalaryProject() {
        //先杀后插
        int i = jxCostMapper.deleteDate();
        log.info("{}",i+"条数据");
        //查询薪酬工资项目列表
        List<JxSalaryProject> list = jxSalaryProjectMapper.listSalary();
        if (list.isEmpty()) {
            throw new RuntimeException("工资项目查询错误");
        }
        //插入
        int fromIndex = 0;
        List<JxSalaryProject> insertList = new ArrayList<>();
        for (JxSalaryProject item : list) {
            fromIndex++;
            insertList.add(item);
            if (fromIndex % 20 == 0 && fromIndex <= list.size()) {
                jxCostMapper.saveSalaryBatch(insertList);
                insertList = new ArrayList<>();
            }
        }
        if (CollectionUtils.isNotEmpty(insertList)) {
            jxCostMapper.saveSalaryBatch(insertList);
        }
        return "success";
    }
}
